/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import controller.AddDevice;
import controller.LabDetail;
import controller.ListLabs;
import entities.Device;
import entities.Lab;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY-THI
 */
@ManagedBean
@ViewScoped
public class AddDeviceBean {

    private int labid;
    private List<Lab> labs;
    private String devicename;
    private String description;

    public int getLabid() {
        return labid;
    }

    public void setLabid(int labid) {
        this.labid = labid;
    }

    public List<Lab> getLabs() {
        ListLabs l = new ListLabs();
        List<Lab> la = l.getLabs();
        List<Lab> ls = new ArrayList<Lab>();
        for (Lab lb : la) {
            if (lb.getStatus()) {
                ls.add(lb);
            }
        }
        return ls;
    }

    public void setLabs(List<Lab> labs) {
        this.labs = labs;
    }

    public String getDevicename() {
        return devicename;
    }

    public void setDevicename(String devicename) {
        this.devicename = devicename;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Creates a new instance of AddDeviceBean
     */
    public AddDeviceBean() {
    }

    public String resultAdd() {
        LabDetail ld = new LabDetail();
        Lab l = ld.labDetail(labid);
        Device de = new Device();
        de.setDevicename(devicename);
        de.setDescription(description);
        de.setIsProlem(Boolean.FALSE);
        de.setLabID(l);
        de.setStatus(Boolean.TRUE);
        de.setCreateDate(new Date());
        AddDevice ad = new AddDevice();
        boolean addDevice = ad.addDevice(de);
        if (addDevice) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Success"));
            return "";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Error please try again later"));
        return "";
    }
}
